package nf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import nf.modular.ModuleHost;
import nf.modular.NFWiring;
import nf.pluginsys.PluginHost;
import nf.pluginsys.Wiring;

public class SettingsStore 
{
	public enum AddBehaviour
	{
		Default,
		StartOnAddOne,
		StartOnAddMany
	}
	
	public enum ResizeMode
	{
		Default,
		NoResize,
		Custom
	}
	
	public static Wiring currentWiring = null;
	public static ModuleHost moduleHost = null;
	
	public static final int DEFAULT_WIDTH = 1400;
	public static final int DEFAULT_HEIGHT = 1056;
	
	public static boolean octaveValidated = false;
	public static String octavePath = "octave";
	public static String thetasPath = "thetas.mat";
	public static AddBehaviour addBehaviour = AddBehaviour.Default;
	public static boolean useEnhancedProcessing = false;
	
	public static File tempFolder = new File("TEMP");
	
	public static PluginHost pluginHost = new PluginHost();
	
	public static ResizeMode resizeMode = ResizeMode.Default;
	public static int imageResizeW = DEFAULT_WIDTH;
	public static int imageResizeH = DEFAULT_HEIGHT;
	
	//TODO multithreading broken
	public static int coreUsage = 1;
	
	public static String lastImageDir = ".";
	public static String lastListDir = ".";
	
	private static boolean useUserDir = true;
	
	private static String userDataDir = "." + File.separator;
	private static String settingsFileName = "neurofrac.ini";
	
	public static void loadSettings()
	{
		if(useUserDir)
		{
			String appData = getUserDataDir();
			userDataDir = appData + File.separator + "NeuroFrac" + File.separator;
			File udir = new File(userDataDir);
			if(!udir.exists())
			{
				System.out.println("FracLac userdata dir not found, creating");
				if(udir.mkdir())
				{
					System.out.println("Userdata dir created");
					userDataDir = udir.getAbsolutePath() + File.separator;
					settingsFileName = "neurofrac.ini"; 
				}
				else
				{
					System.out.println("Couldn't create userdata dir, reverting to local");
					userDataDir = "." + File.separator;
					settingsFileName = "neurofrac.ini";
				}
			}
		}
		else
		{
			userDataDir = "." + File.separator;
			settingsFileName = "neurofrac.ini";
		}
		
		IniParser ini = new IniParser(userDataDir + settingsFileName);
		try { octaveValidated = Boolean.valueOf(ini.getString("octaveValidated", "false")); } catch (Exception e) { octaveValidated = false; }
		octavePath = ini.getString("octavePath", "octave");
		thetasPath = ini.getString("thetasPath", "thetas.mat");
		try { addBehaviour = AddBehaviour.valueOf(ini.getString("addBehaviour", "Default")); } catch (Exception e) { addBehaviour = AddBehaviour.Default; }
		try { useEnhancedProcessing = Boolean.valueOf(ini.getString("useEnhancedProcessing", "false")); } catch (Exception e) { useEnhancedProcessing = false; }
		try { resizeMode = ResizeMode.valueOf(ini.getString("resizeMode", "Default")); } catch (Exception e) { resizeMode = ResizeMode.Default; }
		try { imageResizeW = Integer.parseInt(ini.getString("imageResizeW", Integer.toString(DEFAULT_WIDTH))); } catch (Exception e) { imageResizeW = DEFAULT_WIDTH; }
		try { imageResizeH = Integer.parseInt(ini.getString("imageResizeH", Integer.toString(DEFAULT_HEIGHT))); } catch (Exception e) { imageResizeH = DEFAULT_HEIGHT; }
		//TODO multithreading broken
		try { coreUsage = Integer.parseInt(ini.getString("coreUsage", "1")); } catch (Exception e) { coreUsage = 1; }
		lastImageDir = ini.getString("lastImageDir", ".");
		lastListDir = ini.getString("lastListDir", ".");
		
		//System.out.println("Octave validated from INI : " + ini.getString("octaveValidated", "???"));
		//System.out.println("Octave validated value : " + octaveValidated);
		
		if(resizeMode == ResizeMode.Custom)
		{
			if(imageResizeW < 1 || imageResizeH < 1)
			{
				imageResizeW = DEFAULT_WIDTH;
				imageResizeH = DEFAULT_HEIGHT;
			}
		}
		
		if(coreUsage < 0 || coreUsage > 16)
		{
			coreUsage = 0;
		}
		
		File f1 = new File(lastImageDir);
		if(!f1.exists() || f1.isDirectory()) lastImageDir = ".";
		
		File f2 = new File(lastListDir);
		if(!f2.exists() || f2.isDirectory()) lastListDir = ".";
	}
	
	private static String getUserDataDir()
	{
		String workingDirectory;
		//here, we assign the name of the OS, according to Java, to a variable...
		String OS = (System.getProperty("os.name")).toUpperCase();
		//to determine what the workingDirectory is.
		//if it is some version of Windows
		if (OS.contains("WIN"))
		{
		    //it is simply the location of the "AppData" folder
		    workingDirectory = System.getenv("AppData");
		}
		//Otherwise, we assume Linux or Mac
		else
		{
		    //in either case, we would start in the user's home directory
		    workingDirectory = System.getProperty("user.home");
		    
		    if(OS.contains("LINUX"))
		    {
		    	File fConfig = new File(workingDirectory + "/.config");
		    	if(!(fConfig.exists() && fConfig.isDirectory()))
		    	{
		    		fConfig.mkdir();
		    		workingDirectory += "/.config";
		    	}
		    }
		    else
		    {
			    //if we are on a Mac, we are not done, we look for "Application Support"
			    workingDirectory += "/Library/Application Support";
		    }
		}
		//we are now free to set the workingDirectory to the subdirectory that is our 
		//folder.
		
		//System.out.println("Working dir: \"" + workingDirectory + "\"");
		return workingDirectory;
	}
	
	public static void saveSettings()
	{
		try {
			PrintWriter pw = new PrintWriter(new File(userDataDir + settingsFileName));
			
			pw.println("octaveValidated=" + octaveValidated);
			pw.println("octavePath=" + octavePath);
			pw.println("thetasPath=" + thetasPath);
			pw.println("addBehaviour=" + addBehaviour);
			pw.println("useEnhancedProcessing=" + useEnhancedProcessing);
			pw.println("resizeMode=" + resizeMode);
			pw.println("imageResizeW=" + imageResizeW);
			pw.println("imageResizeH=" + imageResizeH);
			pw.println("coreUsage=" + coreUsage);
			pw.println("lastImageDir=" + lastImageDir);
			pw.println("lastListDir=" + lastListDir);
			
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
